package free.grnadav.jquerymobilegwt.client.page;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class Page extends Composite implements HasWidgets{

	private static PageUiBinder uiBinder = GWT.create(PageUiBinder.class);

	interface PageUiBinder extends UiBinder<Widget, Page> {
	}

	@UiField
	FlowPanel page;
	
	private String id;

	public Page() {
		initWidget(uiBinder.createAndBindUi(this));
		page.getElement().setAttribute("data-role", "page");
	}

	public native void page() /*-{
		$wnd.jQuery('#' + this.@free.grnadav.jquerymobilegwt.client.page.Page::id).page();
	}-*/;

	public native void changePageToMe() /*-{
		var page = $wnd.jQuery('#'+this.@free.grnadav.jquerymobilegwt.client.page.Page::id);
		$wnd.jQuery.mobile.changePage(page);
	}-*/;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
		page.getElement().setAttribute("id", id);
	}
	
	/* pagebeforecreate -- start */
	private native void nativeBindPageBeforeCreate(PageBeforeCreateEvent.Handler ch, PageBeforeCreateEvent event) /*-{
		var page = $wnd.jQuery('#'+this.@free.grnadav.jquerymobilegwt.client.page.Page::id);
		$wnd.jQuery(page).live('pagebeforecreate', function() {
			ch.@free.grnadav.jquerymobilegwt.client.page.PageBeforeCreateEvent.Handler::onPageBeforeCreate(Lfree/grnadav/jquerymobilegwt/client/page/PageBeforeCreateEvent;)(event);
		});
	}-*/;
	
	public void addPageBeforeCreateEventHandler(PageBeforeCreateEvent.Handler ch) {
//		page.addHandler(ch, PageBeforeCreateEvent.getType()); // not working for some reason
		nativeBindPageBeforeCreate(ch, new PageBeforeCreateEvent()); // works
	}
	/* pagebeforecreate -- end */
	
	/* pagecreate -- start */
	private native void nativeBindPageCreate(PageCreateEvent.Handler ch, PageCreateEvent event) /*-{
		var page = $wnd.jQuery('#'+this.@free.grnadav.jquerymobilegwt.client.page.Page::id);
		$wnd.jQuery(page).live('pagecreate', function() {
			ch.@free.grnadav.jquerymobilegwt.client.page.PageCreateEvent.Handler::onPageCreate(Lfree/grnadav/jquerymobilegwt/client/page/PageCreateEvent;)(event);
		});
	}-*/;
	
	public void addPageCreateEventHandler(PageCreateEvent.Handler ch) {
//		page.addHandler(ch, PageBeforeCreateEvent.getType()); // not working for some reason
		nativeBindPageCreate(ch, new PageCreateEvent()); // works
	}
	/* pagecreate -- end */
	
	
	/* pagebeforeshow -- start */
	private native void nativeBindPageBeforeShow(PageBeforeShowEvent.Handler ch, PageBeforeShowEvent event) /*-{
		var page = $wnd.jQuery('#'+this.@free.grnadav.jquerymobilegwt.client.page.Page::id);
		$wnd.jQuery(page).live('pagebeforeshow', function() {
			ch.@free.grnadav.jquerymobilegwt.client.page.PageBeforeShowEvent.Handler::onPageBeforeShow(Lfree/grnadav/jquerymobilegwt/client/page/PageBeforeShowEvent;)(event);
		});
	}-*/;
	
	public void addPageBeforeShowEventHandler(PageBeforeShowEvent.Handler ch) {
		nativeBindPageBeforeShow(ch, new PageBeforeShowEvent()); 
	}
	/* pagebeforeshow -- end */
	
	
	/* pageshow -- start */
	private native void nativeBindPageShow(PageShowEvent.Handler ch, PageShowEvent event) /*-{
		var page = $wnd.jQuery('#'+this.@free.grnadav.jquerymobilegwt.client.page.Page::id);
		$wnd.jQuery(page).live('pageshow', function() {
			ch.@free.grnadav.jquerymobilegwt.client.page.PageShowEvent.Handler::onPageShow(Lfree/grnadav/jquerymobilegwt/client/page/PageShowEvent;)(event);
		});
	}-*/;
	
	public void addPageShowEventHandler(PageShowEvent.Handler ch) {
		nativeBindPageShow(ch, new PageShowEvent()); 
	}
	/* pageshow -- end */

	public FlowPanel getPage() {
		return page;
	}

	@Override
	public void add(Widget w) {
		page.add(w);
	}

	@Override
	public void clear() {
		page.clear();
	}

	@Override
	public Iterator<Widget> iterator() {
		return page.iterator();
	}

	@Override
	public boolean remove(Widget w) {
		return page.remove(w);
	}
	
}
